package com.mall.server.controller;

import com.mall.commons.R;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.server.service.ProductService;
import com.tanhua.model.domain.PmsProduct;
import com.tanhua.model.dto.ProductListDto;
import com.tanhua.model.vo.PageVo;
import com.tanhua.model.vo.PmsProductParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 查询商品
     * @return
     */
    @GetMapping("/list")
    public R  list(ProductListDto productListDto){
        PageVo pagevo =productService.list(productListDto);
        return R.success(pagevo);
    }

    /**
     * 商品列表批量上下架商品
     * @param ids
     * @param publishStatus
     * @return
     */
    @PostMapping("/update/publishStatus")
    public R updatePublishStatus(@RequestParam List<Long> ids, Integer publishStatus){
        if (ids ==null || publishStatus == null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        productService.updatePublishStatus(ids,publishStatus);
        return R.success("");
    }

    /**
     * 批量上下架商品
     * @param ids
     * @param newStatus
     * @return
     */
    @PostMapping("/update/newStatus")
    public R updateNewStatus(@RequestParam List<Long> ids, Integer newStatus){
        if (ids ==null || newStatus == null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        productService.updateNewStatus(ids, newStatus);
        return R.success("");
    }

    /**
     * 批量推荐与否
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public R updateRecommendStatus(@RequestParam List<Long> ids, Integer recommendStatus){
        if (ids ==null || recommendStatus == null){
            throw new BusinessException(ErrorResult.parameterError());
        }
        productService.updateRecommendStatus(ids, recommendStatus);
        return R.success("");
    }


    /**
     * 创建商品
     * @param pmsProductParam
     * @return
     */
    @PostMapping("/create")
    public R productCreate(@RequestBody PmsProductParam pmsProductParam){
        productService.productCreate(pmsProductParam);
        return R.success("");
    }

    /**
     * 修改商品时回显
     * @param id
     * @return
     */
    @GetMapping("/updateInfo/{id}")
    public R updateInfoById(@PathVariable("id")Long id){
        PmsProductParam pmsProductParam =productService.updateInfoById(id);
        return R.success(pmsProductParam);
    }

    /**
     * 删除商品
     */
    @PostMapping("/update/deleteStatus")
    public R updateDeleteStatus(@RequestParam List<Long> ids,Integer deleteStatus){
        productService.updateDeleteStatus(ids,deleteStatus);
        return R.success("");
    }

    /**
     * 更新商品
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public R updateById(@PathVariable("id") Long id,@RequestBody PmsProductParam pmsProductParam){
        productService.updateById(id,pmsProductParam);
        return R.success("");
    }

    /**
     * 根据商品名称或货号模糊查询
     * @param keyword
     * @return
     */
    @GetMapping("/simpleList")
    public R productSimpleList(String keyword){
        List<PmsProduct> pmsProduct =productService.productSimpleList(keyword);
        return R.success(pmsProduct);
    }


}
